# -*- coding: utf-8 -*-
# @Date：2024/9/30 11:54:07
# @Author：CJiaの青姝
# @FileName：cj_response
# @Editor：PyCharm2024
# @Remark：
from django.http import JsonResponse
from utils.cj_jwt import DateEncoder


class SuccessResponse(JsonResponse):
    """
    标准响应成功的返回, SuccessResponse(data)或者SuccessResponse(data=data)
    (1)默认code返回200, 不支持指定其他返回码
    """

    def __init__(self, data=None, msg='操作成功🌻', code=200, *args, **kwargs):
        std_data = {
            "code": code,
            "data": data,
            "msg": msg,
            "success": True
        }
        # 响应JSON格式数据
        super().__init__(std_data, safe=False, encoder=DateEncoder, *args, **kwargs)


class ErrorResponse(JsonResponse):
    """
    标准响应错误的返回,ErrorResponse(msg='xxx')
    (1)默认错误码返回400, 也可以指定其他返回码:ErrorResponse(code=xxx)
    """

    def __init__(self, data=None, msg='操作失败🌻', code=400, *args, **kwargs):
        std_data = {
            "code": code,
            "data": data,
            "msg": msg,
            "success": False
        }
        # 响应JSON格式数据
        super().__init__(std_data, safe=False, encoder=DateEncoder, *args, **kwargs)
